package com.sunyard.client2.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.sunyard.client.SunEcmClientApi;
import com.sunyard.client.bean.ClientBatchBean;
import com.sunyard.client.bean.ClientFileBean;
import com.sunyard.client2.ConnectionFactory;
import com.sunyard.client2.conf.ConfUtil;
import com.sunyard.util.OptionKey;

public class UpdateAddFile extends JPanel {
	private JTextField textField;

	/**
	 * Create the panel.
	 */
	public UpdateAddFile() {
		setLayout(null);

		JLabel label = new JLabel("文件：");
		label.setBounds(82, 53, 72, 15);
		add(label);

		textField = new JTextField();
		textField.setBounds(164, 50, 411, 21);
		add(textField);
		textField.setColumns(10);

		JButton button = new JButton("选择");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				fc.setCurrentDirectory(new File(ConfUtil.getString("dir", "C:/")));
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int returnVal = fc.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					textField.setText(file.getPath());
				}

			}
		});
		button.setBounds(578, 49, 93, 23);
		add(button);

		JButton button_1 = new JButton("新增");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientFileBean fileBean1 = new ClientFileBean();
				fileBean1.setOptionType(OptionKey.U_ADD);
				fileBean1.setFileName(textField.getText());
				fileBean1.setFileFormat(textField.getText().substring(textField.getText().lastIndexOf(".") + 1));
				File f = new File(textField.getText());
				fileBean1.setFilesize(String.valueOf(f.length()));

				ClientBatchBean bean = batchInfoPanel.getBatchBean();
				bean.getDocument_Objects().get(0).addFile(fileBean1);
				try {
					SunEcmClientApi clientApi = ConnectionFactory.getConnection();
					String resultMsg = clientApi.update(bean, ConfUtil.getString("groupName", ""), true);
					ResultMessageBox.getInstance().showMessage(resultMsg);
				} catch (Exception e1) {
					ResultMessageBox.getInstance().showMessage(e1.getMessage());
				}
			}
		});
		button_1.setBounds(139, 201, 93, 23);
		add(button_1);

		JButton btnNewButton = new JButton("覆盖");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientFileBean fileBean1 = new ClientFileBean();
				fileBean1.setOptionType(OptionKey.U_REPLACE);
				fileBean1.setFileNO(textField_1.getText());
				fileBean1.setFileName(textField.getText());
				fileBean1.setFileFormat(textField.getText().substring(textField.getText().lastIndexOf(".") + 1));
				File f = new File(textField.getText());
				fileBean1.setFilesize(String.valueOf(f.length()));

				ClientBatchBean bean = batchInfoPanel.getBatchBean();
				bean.getDocument_Objects().get(0).addFile(fileBean1);
				try {
					SunEcmClientApi clientApi = ConnectionFactory.getConnection();
					String resultMsg = clientApi.update(bean, ConfUtil.getString("groupName", ""), true);
					ResultMessageBox.getInstance().showMessage(resultMsg);
				} catch (Exception e1) {
					ResultMessageBox.getInstance().showMessage(e1.getMessage());
				}
			}
		});
		btnNewButton.setBounds(319, 201, 93, 23);
		add(btnNewButton);

		JButton button_2 = new JButton("删除");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientFileBean fileBean1 = new ClientFileBean();
				fileBean1.setOptionType(OptionKey.U_DEL);
				fileBean1.setFileNO(textField_1.getText());

				ClientBatchBean bean = batchInfoPanel.getBatchBean();
				bean.getDocument_Objects().get(0).addFile(fileBean1);
				try {
					SunEcmClientApi clientApi = ConnectionFactory.getConnection();
					String resultMsg = clientApi.update(bean, ConfUtil.getString("groupName", ""), true);
					ResultMessageBox.getInstance().showMessage(resultMsg);
				} catch (Exception e1) {
					ResultMessageBox.getInstance().showMessage(e1.getMessage());
				}
			}
		});
		button_2.setBounds(493, 201, 93, 23);
		add(button_2);

		JLabel label_1 = new JLabel("文件编号：");
		label_1.setBounds(82, 97, 75, 15);
		add(label_1);

		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(164, 94, 411, 21);
		add(textField_1);

	}

	private UpdateBatchInfo batchInfoPanel;
	private JTextField textField_1;

	public UpdateAddFile(UpdateBatchInfo batchInfoPanel) {
		this();
		this.batchInfoPanel = batchInfoPanel;
	}

}
